/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.server.database;

import com.server.main.ClientManager;
import java.io.File;
import java.sql.Timestamp;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author daz
 */
public class DataEngine implements IDataEngine {

    private TableDao tableDao;
    private String host = "202.92.4.22";
//    private String host = "localhost";
    private int port = 3306;
    private String databaseUser = "gamepoker";
    private String databasePass = "GaMe2o12";
    private String databaseSchema = "poker_online";
    private String databaseName = "poker_online";
    private static DataEngine dataEngine;

    private DataEngine() {
    }

    public static DataEngine getInstance() {
        if (dataEngine == null) {
            dataEngine = new DataEngine();
        }
        return dataEngine;
    }

    @Override
    public void setConfig(String host, int port, String user, String pass, String name, String schema) {
        try {
//            System.out.println("host :" + host);
//            System.out.println("port :" + port);
//            System.out.println("user :" + user);
//            System.out.println("name :" + name);
//            System.out.println("pass :" + pass);
            this.host = host;
            this.port = port;
            this.databaseUser = user;
            this.databasePass = pass;
            this.databaseName = name;
            this.databaseSchema = schema;
        } catch (Exception ex) {
        }
    }

    public long getUserMoney(String name) {
        List<MoneyInfo> list = tableDao.getUserMoney(name);
        if (list.isEmpty()) {
            return 0;
        }

        return list.get(0).getMoney();
    }

    public void connectToDabatabase() {
        try {
            String fs = File.separator;
            String folderConfig = System.getProperty("user.dir") + fs + "BGATEPOKER" + fs + "dbconfig" + fs;
            ApplicationContext ac = new ClassPathXmlApplicationContext("file:" + folderConfig + "context.xml");
            DataSource dataSource = (DataSource) ac.getBean("mysqlDataSource");
            tableDao = new TableDao(dataSource, databaseSchema);
        } catch (Exception ex) {
        }
    }

    public UserInfo getUserInfo(String user) {
        List<UserInfo> list = tableDao.getUserInfo(user);
        if (list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }

    public List<ChildInfo> getChildInfo(String userName) {
        List<ChildInfo> childList = tableDao.getChildInfo(userName);
        return childList;
    }

    public void updateLastLogin(String userName) {
        tableDao.updateLastLogin(userName);
    }

    public int testConnection() {
        return this.tableDao.testConnection();
    }

    public void resetUserIsplay() {
        this.tableDao.resetUserIsplay();
    }

    public void changeLoginId(long id, String userName) {
        this.tableDao.changeLoginId(id, userName);
    }

    public void setUserOnline(int state, String userName) {
        this.tableDao.setUserOnline(state, userName);
    }

    public void insertUser(String userName, String pass, String gender, String companyId) {
        this.tableDao.insertUserRegister(userName, pass, gender, companyId);
    }

    public List<UserInfo> findUserName(String userName) {
        return this.tableDao.getUserRegisterInfo(userName);
    }

    public void updateMoney(long moneyChange, String userName) {
        this.tableDao.updateMoney(moneyChange, userName);
    }

    public MateInfo getMateInfo(String name) {
        List<MateInfo> mateList = this.tableDao.getMateInfo(name);
        if (mateList.size() > 0) {
            return mateList.get(0);
        }
        return null;
    }

    public void updateLevel(String userName, int newLevel) {
        this.tableDao.updateLevel(userName, newLevel);
    }

    public void updateWin(String userName, int newWin) {
        this.tableDao.updateWin(userName, newWin);
    }

    public void updateLose(String userName, int newLose) {
        this.tableDao.updateLose(userName, newLose);
    }

    public void insetNewFriend(String userName1, String userName2) {
        this.tableDao.insertNewFriend(userName1, userName2);
    }

    public void marry(String userName1, String userName2) {
        this.tableDao.marry(userName1, userName2);
    }

    public boolean checkFriend(String userName1, String userName2) {
        List<FriendInfo> list = this.tableDao.checkMaried(userName1, userName2);
        if (list.isEmpty()) {
            return false;
        }
        return true;
    }

    public List<FriendInfo> getListFriend(String userName) {
        return this.tableDao.getListFriend(userName);
    }

    public void insertNewMessenger(String userSend, String userReceive, String message, Timestamp timeSend, int kind) {
        this.tableDao.insertNewMessenger(userSend, userReceive, message, timeSend, kind);
    }

    public List<UserMessage> getMessage(String userReceive) {
        return this.tableDao.getMessage(userReceive);
    }

    public List<UserInfo> getTopRich() {
        return this.tableDao.getTopRich();
    }

    public List<UserInfo> getTopLevel() {
        return this.tableDao.getTopLevel();
    }

    public void insertUserStatus(String username, String status) {
        this.tableDao.insertUserStatus(username, status);
    }

    public List<ListItem> getItemInfo() {
        return this.tableDao.getItemInfo();
    }

    public void insertNewBuyItem(int isActivated, int kind, String userName, Timestamp time) {
        this.tableDao.insertNewBuyItem(isActivated, kind, userName, time);
        this.tableDao.insertNewItemHistory(kind, ClientManager.BUY_ITEM, userName);
    }

    public List<Item> getUserListItem(String name) {
        return this.tableDao.getUserListItem(name);
    }

    public List<ShopHistoryItem> getUserShopHistory(String name) {
        return this.tableDao.getUserShopHistory(name);
    }

    public void activateItem(Timestamp newTime, Timestamp oldTime, String userName, int kind) {
        this.tableDao.activateItem(newTime, oldTime, userName, kind);
        this.tableDao.insertNewItemHistory(kind, ClientManager.APP_ITEM, userName);
    }

    public void deleteItemUsed(String name, Timestamp timeUse) {
        this.tableDao.deleteItemUsed(name, timeUse);
    }

    public void marryUser(String userName, String friend) {
        this.tableDao.marryTwoUser(userName, friend);
    }

    public void insertChild(int familyId, String childName, int childGender) {
        this.tableDao.inserChild(familyId, childName, childGender);
    }

    public List<Test> testUnicode() {
        return this.tableDao.testUnicode();
    }

    public void deleteMessage(String userSend, String userReceive, Timestamp time) {
        this.tableDao.deleteMessage(userSend, userReceive, time);
    }

    public void deleteAllMarryMessage(String userName) {
        this.tableDao.deleteAllMarryMessage(userName);
    }

    public void deleteAllBirthMessage(String userName) {
        this.tableDao.deleteAllBirthMessage(userName);
    }

    public void setReadMessage(String userSend, String userReceive, Timestamp timeSend) {
        this.tableDao.setReadMessage(userSend, userReceive, timeSend);
    }

    public void setAvatar(int val, String name) {
        this.tableDao.setAvatar(val, name);
    }

    public List<SmsInfo> getSmsQueue() {
        return this.tableDao.getSmsQueue();
    }

    public void logTransaction() {
        this.tableDao.logTransaction();
    }

    public void changePass(String userName, String newPass) {
        this.tableDao.changePass(userName, newPass);
    }

    public void activateAcc(String name, int companyId) {
        this.tableDao.activateAcc(name, companyId);
    }

    public void deleteFriend(String userName, String friend) {
        this.tableDao.deleteFriend(userName, friend);
    }

    public void logUseCard(String issuerName, String cardSerial, String cardCode, int result, String name) {
//        Debug.d("result:" + result);
        this.tableDao.logUserCard(issuerName, cardSerial, cardCode, result, name);
    }

    public int getTransRef() {
        return this.tableDao.getTransRef();
    }

    public void logUseCardAmount(String cardSerial, String cardCode, long transRef, String amount) {
        this.tableDao.logUseCardAmount(cardSerial, cardCode, amount);
    }

    public int countSmsLimit(String userName) {
        return this.tableDao.countSms(userName);
    }

    public int sumSmsCost(String userName) {
        return this.tableDao.sumSmsCost(userName);
    }

    public int isRegisted(String phoneNumber) {
        return this.tableDao.isRegisted(phoneNumber);
    }

    public CompanyInfo getSmsInfo(int companyId) {
        List<CompanyInfo> list = tableDao.getSMSInfo(companyId);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public void enterServer(String clientName, long clientId, String device, int numberLogin) {
        tableDao.enterServer(clientName, clientId, device, numberLogin);
    }

    public void exitServer(String clientName) {
        tableDao.exitServer(clientName);
    }

    void updateGender(int id, int gender) {
        tableDao.updateGender(id, gender);
    }

    public int checkActivate(String clientName) {
        return tableDao.checkActivate(clientName);
    }

    public List<CompanyInfo> getListCp() {
        return tableDao.getListCp();
    }
}
